package handlers

import (
	"code/config"
	"code/utils"
	"fmt"
	"strings"

	"github.com/fatih/color"
)

// 定义全局颜色变量
var (
	//red   = color.New(color.FgRed).Add(color.Bold)
	green = color.New(color.FgGreen)
	blue  = color.New(color.FgBlue)
	// 可以根据需要添加更多颜色
)

// ListHandler 处理根据给定配置生成代码的逻辑
func ListHandler(cfg config.Config) config.CodeResult {
	var codes config.CodeResult
	blue.Printf("开始生成 %s 的代码\n", cfg.Action)

	codeController := controllerBuild(cfg)
	codeControllerContent := fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, codeController[0], cfg.EndTag)

	codes.ControllerCode = codeControllerContent
	codes.ServiceCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceBuild(cfg)[0], cfg.EndTag)
	codes.ServiceImplCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceImplBuild(cfg)[0], cfg.EndTag)
	codes.MapperCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, mapperBuild(cfg)[0], cfg.EndTag)
	codes.MapperXMLCode = fmt.Sprintf("<!--%s-->\n%s\n<!--%s-->\r\n\n", cfg.StartTag, mapperXMLBuild(cfg)[0], cfg.EndTag)

	green.Printf("生成完毕\n%s\n", cfg.Name)
	return codes
}

// 控制器构建函数
func controllerBuild(config config.Config) []string {
	controllerTemplate := `
// 控制器
@{ApiType}Mapping("{Api}")
public TableDataInfo list{tag}({Name} {nameVariable}) {
    startPage();
    List<{Name}> list = {service}.select{Name}List{tag}({nameVariable});
    return getDataTable(list);
}
`
	//config.ApiType 改成大驼峰
	controllerCode := strings.ReplaceAll(controllerTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{tag}", config.Tag)
	controllerCode = strings.ReplaceAll(controllerCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{service}", utils.ToCamelCase(config.Table)+"Service")
	controllerCode = strings.ReplaceAll(controllerCode, "{ApiType}", utils.ToPascalCase(config.ApiType))
	controllerCode = strings.ReplaceAll(controllerCode, "{Api}", config.Api)

	return []string{controllerCode}
}

// 服务构建函数
func serviceBuild(config config.Config) []string {
	serviceTemplate := `
/**
 * 查询 {Name}列表服务
 * 
 * @param {Name} {nameVariable}
 * @return {Name}列表
 */
public List<{Name}> select{Name}List{tag}({Name} {nameVariable});
`
	serviceCode := strings.ReplaceAll(serviceTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceCode = strings.ReplaceAll(serviceCode, "{tag}", config.Tag)
	serviceCode = strings.ReplaceAll(serviceCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{serviceCode}
}

// 服务实现构建函数
func serviceImplBuild(config config.Config) []string {
	serviceImplTemplate := `
//{Name}服务实现	
@Override
public List<{Name}> select{Name}List{tag}({Name} {nameVariable}) {
    return {mapper}.select{Name}List{tag}({nameVariable});
}
`
	serviceImplCode := strings.ReplaceAll(serviceImplTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{tag}", config.Tag)
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{mapper}", utils.ToCamelCase(config.Table)+"Mapper")

	return []string{serviceImplCode}
}

// Mapper构建函数
func mapperBuild(config config.Config) []string {
	mapperTemplate := `
// Mapper
/**
 * 查询{Name}列表
 * 
 * @param {Name} {nameVariable}
 * @return {Name}集合
 */
public List<{Name}> select{Name}List{tag}({Name} {nameVariable});
`
	mapperCode := strings.ReplaceAll(mapperTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperCode = strings.ReplaceAll(mapperCode, "{tag}", config.Tag)
	mapperCode = strings.ReplaceAll(mapperCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{mapperCode}
}

// Mapper XML构建函数
func mapperXMLBuild(config config.Config) []string {
	mapperXMLTemplate := `
<select id="select{Name}List{tag}" parameterType="{Name}" resultMap="{Name}Result">
    <include refid="select{Name}Vo"/>
    <where>  
		<if test="sort != null "> and sort = #{sort}</if>
		<if test="isEnable != null "> and is_enable = #{isEnable}</if>
		<if test="title != null  and title != ''"> and title = #{title}</if>
		<if test="displayTime != null "> and display_time = #{displayTime}</if>
		<if test="cover != null  and cover != ''"> and cover = #{cover}</if>
		<if test="content != null  and content != ''"> and content = #{content}</if>
		<if test="info != null  and info != ''"> and info = #{info}</if>
		<!-- 新增条件 -->
		<if test="copyfrom != null  and copyfrom != ''"> and copyfrom = #{copyfrom}</if>
		<if test="keyword != null  and keyword != ''"> and keyword like CONCAT('%', #{keyword}, '%')</if>
		<if test="focus != null "> and focus = #{focus}</if>
		<if test="sug != null "> and sug = #{sug}</if>
    </where>
    ORDER BY sort DESC, id DESC
</select>
`
	mapperXMLCode := strings.ReplaceAll(mapperXMLTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{tag}", config.Tag)
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{table}", config.Table)

	return []string{mapperXMLCode}
}
